{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Latent Dirichlet Allocation - Interactive Simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-20T19:29:31.593968Z",
     "start_time": "2020-06-20T19:29:31.592252Z"
    }
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-20T19:29:32.049786Z",
     "start_time": "2020-06-20T19:29:31.595196Z"
    },
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# Visualization\n",
    "from ipywidgets import interact, FloatSlider\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-20T19:29:32.052552Z",
     "start_time": "2020-06-20T19:29:32.050811Z"
    }
   },
   "outputs": [],
   "source": [
    "sns.set_style('white')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Simulate Dirichlet Distribution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-06-20T19:29:33.473212Z",
     "start_time": "2020-06-20T19:29:32.053516Z"
    },
    "hide_input": false,
    "scrolled": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dc49fc91f2a84c99ad27562e5f2c4278",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=1.0, continuous_update=False, description='Alpha', max=10.0, min=0.01,…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "f = FloatSlider(value=1,\n",
    "                min=1e-2,\n",
    "                max=10,\n",
    "                step=1e-2,\n",
    "                continuous_update=False,\n",
    "                description='Alpha')\n",
    "\n",
    "\n",
    "@interact(alpha=f)\n",
    "def sample_dirichlet(alpha):\n",
    "    topics = 10\n",
    "    draws = 9\n",
    "    alphas = np.full(shape=topics, fill_value=alpha)\n",
    "    samples = np.random.dirichlet(alpha=alphas, size=draws)\n",
    "\n",
    "    fig, axes = plt.subplots(nrows=3,\n",
    "                             ncols=3,\n",
    "                             sharex=True,\n",
    "                             sharey=True,\n",
    "                             figsize=(14, 8))\n",
    "    axes = axes.flatten()\n",
    "    plt.setp(axes, ylim=(0, 1))\n",
    "    for i, sample in enumerate(samples):\n",
    "        axes[i].bar(x=list(range(10)), height=sample,\n",
    "                    color=sns.color_palette(\"Set2\", 10))\n",
    "    fig.suptitle('Dirichlet Allocation | 10 Topics, 9 Samples')\n",
    "    sns.despine()\n",
    "    fig.tight_layout()\n",
    "    plt.subplots_adjust(top=.95)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "name": "_merged",
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": true,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "100px",
    "left": "85.9915px",
    "right": "1064px",
    "top": "66.3352px",
    "width": "246.335px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
